/**
 * 
 */
package es.ats.codington.festival.services;

import java.sql.SQLException;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import es.ats.codington.festival.daos.ManagerDAO;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.People;
import es.ats.codington.festival.domain.Visitor;

import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;
import es.ats.codington.festival.interfaces.daos.IVisitorDAO;
import es.ats.codington.festival.interfaces.services.IVisitorService;


/**
 * @author Codington_RED
 *
 */
public class VisitorService implements IVisitorService {
	
	
	private IVisitorDAO visitorDAO;
	private IEventSignUpDAO eventSignUpDAO;
	
	public VisitorService(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
		ManagerDAO daoManager = (ManagerDAO) ctx.getBean("daoManager");
		this.visitorDAO = daoManager.getVisitorDAO();
	}
	

	
	/**
	 * method that obtains the ID from a Person 
	 * @param people
	 * @return visitorDAO.getIdPeople(people)
	 * @throws ServiceException
	 * 
	 */
	@Override
	public int getIdPeople(People people) throws ServiceException {
		
		return visitorDAO.getIdPeople(people);
		
	}
	
	
	/**
	 * method that obtains the ID from a visitor
	 * @param visitor
	 * @return visitorDAO.getIdVisitor(visitor)
	 * @throws ServiceException
	 */
	@Override
	public int getIdVisitor(Visitor visitor) throws ServiceException {
		
		return visitorDAO.getIdVisitor(visitor);

	}
	
	
	/**
	 * method that checks if a visitor is an admin or not
	 * @param visitor
	 * @return visitorDAO.getCheckAdmin(visitor)
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean checkAdmin(Visitor visitor) throws ServiceException, SQLException {
		
		return visitorDAO.getCheckAdmin(visitor);
		
	}
	
	
	/**
	 * method that checks if a visitor have logged rightly 
	 * @param visitor
	 * @return visitorDAO.getCheckLogin(visitor)
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean checkLogin(Visitor visitor) throws ServiceException, SQLException {
		
		return visitorDAO.getCheckLogin(visitor);

	}
	
	
	/**
	 * method that checks if a people have been logged
	 * @param people
	 * @return visitorDAO.getCheckPeople(people)
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean checkPeople(People people) throws ServiceException, SQLException {
		
		return visitorDAO.getCheckPeople(people);

	}
	
	
	/**
	 * method that creates a visitor
	 * @param people
	 * @return visitorDAO.insertData(people)
	 * @throws ServiceException
	 */
	@Override
	public boolean createVisitor(People people) throws ServiceException{
		
		return visitorDAO.insertData(people);
		
	}

	/**
	 * method that creates a visitor
	 * @param visitor
	 * @return visitorDAO.insertData(visitor)
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean createVisitor(Visitor visitor) throws ServiceException {
		
		return visitorDAO.insertData(visitor);
		
	}
	/**
	 * method that creates a visitor
	 * @param visitor
	 * @return visitorDAO.insertDataV(visitor)
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean createVisitorV(Visitor visitor) throws ServiceException {
		
		return visitorDAO.insertDataV(visitor);
		
	}

	/**
	 * method that searches a visitor has been logged
	 * @param visitor
	 * @return locatedVisitor
	 * @throws ServiceException
	 */
	@Override
	public Visitor searchVisitor(Visitor visitor) throws ServiceException {
		
		return visitorDAO.searchVisitor(visitor);
		
	}
	
	
	/**
	 * method that searches a visitor has been logged
	 * @param people
	 * @return visitorDAO.searchPeople(people)
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public People searchPeople(People people) throws ServiceException {
		
		return visitorDAO.searchPeople(people);
		
	}

	/**
	 * method that shows the events which the visitor are registered in
	 * @param visitor
	 * @return eventSignUpDAO.registeredEvents(eventSignUp)
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public List<Event> showRegisteredEventsForVisitor(Visitor visitor) throws ServiceException {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
		ManagerDAO daoManager = (ManagerDAO) ctx.getBean("daoManager");
		this.eventSignUpDAO = daoManager.getEventSignUpDAO();
		
		EventSignUp eventSignUp = new EventSignUp();
		eventSignUp.setIdVisitor(visitor.getVisitorId());
		
		return eventSignUpDAO.registeredEvents(eventSignUp);
		
		
	}

	
	/**
	 * method that modifies the information of a visitor
	 * @param visitor
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public int updateVisitor(Visitor visitor) throws ServiceException {
		
		return visitorDAO.updateVisitor(visitor);
		
	}

	
	/**
	 * method that modifies the information of a visitor
	 * @param people
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public int updateVisitor(People people) throws ServiceException {
		
		return visitorDAO.updateVisitor(people);
		
	}
	
	
	/**
	 * method that modifies the DNI data of a visitor
	 * @param people
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public int updateVisitorDni(People people) throws ServiceException {
		
		return visitorDAO.updateVisitorDni(people);
		
	}
	
	/**
	 * method that modifies the password information of a visitor
	 * @param visitor
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public int changePassword(Visitor visitor) throws ServiceException {
		
		return visitorDAO.changePassword(visitor);
		
	}
	
	/**
	 * method that implements a list of the events which the visitor are registered in
	 * @param visitor
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
    public List<Event> showRegisteredEvents(Visitor visitor) throws ServiceException{
		
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
		ManagerDAO daoManager = (ManagerDAO) ctx.getBean("daoManager");
		this.eventSignUpDAO = daoManager.getEventSignUpDAO();
		
		EventSignUp eventSignUp = new EventSignUp();
		eventSignUp.setIdVisitor(visitor.getVisitorId());
		
		return eventSignUpDAO.registeredEvents(eventSignUp);
	
    }
	
}
